package com.wei.tang.db;

import java.util.ArrayList;
import java.util.List;

import com.wei.tang.bean.ClassifyBean;
import com.wei.tang.bean.UserBean;
import com.wei.tang.db.DBInfo.ClassifyTable;
import com.wei.tang.db.DBInfo.UserTable;
import com.wei.tang.util.Tlog;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.test.AndroidTestCase;

/**
 * 
 * @author 吴佳明
 *
 */
public class ClassifyDao extends AndroidTestCase {

	private DBHelper dbHelper = null;
	private SQLiteDatabase db = null;
	private ContentValues values = null;
	String[] columns = { DBInfo.UserTable.USER_NAME };

	public ClassifyDao(Context context) {
		Tlog.i("进入数据库类");
		dbHelper = new DBHelper(context);
		Tlog.i("数据库  表创建成功");
	}

	public ClassifyBean classifyRegist(String _classify_id, String _classify_name, String _classify_desc,
			String _classify_addtime, String _classify_adduserid) {
		Tlog.i("用户注册");
		ClassifyBean userBean = null;
		// judge user regist yes return null
		if (findUserByPhone(_classify_name) != null) {
			return userBean;
		}
		userBean = new ClassifyBean(_classify_id, _classify_name, _classify_desc, _classify_addtime,
				_classify_adduserid);
		if (insertUser(userBean)) {
		} else {
			userBean = null;
		}
		return userBean;
	}

	public boolean insertUser(ClassifyBean classifyBean) {
		Tlog.i("插入用户数据到数据库中");
		db = dbHelper.getReadableDatabase();
		values = fillContentValues(classifyBean);
		long rowId = db.insert(ClassifyTable.CLASSIFY_TABLE, ClassifyTable.CLASSIFY_TABLE, values);
		db.close();
		Tlog.i("是否存成功：" + rowId);
		if (rowId == -1) {
			return false;
		} else {
			return true;
		}
	}

	private ContentValues fillContentValues(ClassifyBean _classifyBean) {
		ContentValues tempValues = new ContentValues();
		tempValues.put(DBInfo.ClassifyTable.CLASSIFY_ID, _classifyBean.getClassify_id());
		tempValues.put(DBInfo.ClassifyTable.CLASSIFY_NAME, _classifyBean.getClassify_name());
		tempValues.put(DBInfo.ClassifyTable.CLASSIFY_DESC, _classifyBean.getClassify_desc());
		tempValues.put(DBInfo.ClassifyTable.CLASSIFY_ADDTIME, _classifyBean.getClassify_addtime());
		tempValues.put(DBInfo.ClassifyTable.CLASSIFY_ADDUSERID, _classifyBean.getClassify_adduserid());
		return tempValues;
	}

	public ClassifyBean findUserByPhone(String user_phone) {
		String sql = "user_phone=? ";
		String[] strs = { user_phone };
		return findUserBySql(sql, strs);
	}

	public ClassifyBean findUserBySql(String sql, String[] strs) {
		Tlog.i("findUserBySql---sql:" + sql);
		db = dbHelper.getReadableDatabase();
		Cursor cur = null;
		ClassifyBean classifyBean = null;
		try {
			cur = db.query(ClassifyTable.CLASSIFY_TABLE, null, sql, strs, null, null, null, null);
			if (cur.moveToNext()) {
				classifyBean = fillUserBean(cur);
				Tlog.i("findUserBySql---sql:" + classifyBean);
			}
			cur.close();
		} catch (Exception e) {
			Tlog.e("e:" + e.getMessage());
		}
		db.close();
		return classifyBean;
	}

	private ClassifyBean fillUserBean(Cursor cur) {
		ClassifyBean classifyBean = new ClassifyBean();
		classifyBean.setClassify_id(cur.getString(cur.getColumnIndex(ClassifyTable.CLASSIFY_ID)));
		classifyBean.setClassify_name(cur.getString(cur.getColumnIndex(ClassifyTable.CLASSIFY_NAME)));
		classifyBean.setClassify_desc(cur.getString(cur.getColumnIndex(ClassifyTable.CLASSIFY_DESC)));
		classifyBean.setClassify_addtime(cur.getString(cur.getColumnIndex(ClassifyTable.CLASSIFY_ADDTIME)));
		classifyBean.setClassify_adduserid(cur.getString(cur.getColumnIndex(ClassifyTable.CLASSIFY_ADDUSERID)));
		return classifyBean;
	}

}
